import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

class SwingExample implements Runnable {

	@Override
	public void run() {
        JFrame f = new JFrame ("Hello, World!");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        f.setJMenuBar(this.createMenuBar());
        f.add(createToolBar(), BorderLayout.PAGE_START);
        
        f.setSize(new Dimension(450, 260));
        f.setVisible(true);
	}
	
	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu menu = new JMenu("Menu One");
		menu.getAccessibleContext().setAccessibleDescription("Top menu item");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("Item One");
		menuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, "Item One is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		});
		menu.add(menuItem);
		
		menu.addSeparator();
		JMenu subMenu = new JMenu("Sub Menu");
		JMenuItem subItemOne = new JMenuItem("Sub Item One");
		subItemOne.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Sub Item One is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		});
		subMenu.add(subItemOne);
		JMenuItem subItemTwo = new JMenuItem("Sub Item Two");
		subItemTwo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Sub Item Two is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		});
		subMenu.add(subItemTwo);
		menu.add(subMenu);
		
		return menuBar;
	}
	
	private JToolBar createToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.add(makeButton("Previous", "go previous", new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Previous Button on the ToolBar is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		}));

		toolBar.add(makeButton("Fresh", "fresh current", new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Fresh Button on the ToolBar is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		}));

		toolBar.add(makeButton("Back", "go back", new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Back Button on the ToolBar is clicked", "Message", JOptionPane.INFORMATION_MESSAGE);
			}
			
		}));
		return toolBar;
	}
	
	private JButton makeButton(String imageName, String tipString, ActionListener listener) {
		JButton button = new JButton();
		button.setToolTipText(tipString);
		button.setText(imageName);
		button.addActionListener(listener);
		return button;
	}
	
	public static void main(String args[]) {
        SwingUtilities.invokeLater(new SwingExample());
	}

}
